<% var style = locals.style === 'homepage' ? 'main-item main-item--bottom' : 'g-footer' %>
<div class="<%= style %>">
  <% if (locals.pager) { %>
    <% pager.forEach((page, i) => { %>
  <a class="button btn-footer" href="<%- formatUrl(page.link) %>"><%= i + 1 %></a>
    <% }) %>
  <% } %>
  <% if (locals.editLink) { %>
    <a class="button btn-footer" href="<%- editLink %>" id="edit-button" target="_blank"><%- template('footer.buttons.edit') %></a>
  <% } %>
  <% if (locals.parentId) { %>
    <a class="button btn-footer" href="https://docs.google.com/document/u/0/create?usp=drive_web&folder=<%- parentId %>" %> target="_blank"><%- template('footer.buttons.create') %></a>
    <a class="button btn-footer" href="https://drive.google.com/drive/u/0/folders/<%- parentId %>" target="_blank"><%- template('footer.buttons.viewFolder') %></a>
  <% } %>

  <p class="help-text"><%- template('footer.helpText') %></p>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.jquery.min.js"></script>
<script>
  // Google Analytics tracking
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  <% if (process.env.GA_TRACKING_ID) { %>
  ga('create', '<%= process.env.GA_TRACKING_ID %>', 'auto');
  <% } %>

  <% if(locals.pageType) { %>
    ga('set', 'contentGroup1', <%- JSON.stringify(locals.pageType) %>);
  <% } %>

  <% if(locals.topLevelFolder) { %>
    ga('set', 'contentGroup2', <%- JSON.stringify(locals.topLevelFolder) %>);
  <% } %>

  // get the userinfo then fire a pageview (can't cache in the page)
  $.ajax({
    method: 'GET',
    url: '<%= formatUrl("/whoami.json") %>'
  }).always(function (data) {
    var userId = (data || {}).analyticsUserId;
    if (userId) {
      ga('set', 'userId', userId)

      if(window.location.pathname === '<%= formatUrl("/") %>') {
        $(document).ready(function() {
          personalizeHomepage(userId)
        })
      }
    }

    ga('send', 'pageview');
    renderUserInfo(data);
  });

  function renderUserInfo(data) {
    var username = data.email || template('footer.defaultEmail');
    $('.user-fullname').text(username);

    var initials = username.split('@')[0].split('.').map(function(name) {
      return name[0].toUpperCase();
    });

    $('.btn-user-initial').text(initials.join(''));
  }

  // ajax for latest filenames, req will 304 and serve cache if unchanged
  var lastFilenameFetch = getFilenameStorage()
  var now = new Date().getTime()
  if (!lastFilenameFetch || now - lastFilenameFetch.lastFetched > 600000) { // 10 min
    $.ajax({
      method: 'GET',
      url: '<%= formatUrl("/filename-listing.json") %>',
      json: true
    }).then(function(data, textStatus, xhr) {
      if (xhr.status !== 200) return; // if ajax fails, continue with old listing

      var lastFetched = new Date().getTime()
      var uniqueNames = new Set(data.filenames)
      var data = {lastFetched: lastFetched, filenames: Array.from(uniqueNames)}
      localStorage.setItem('filenames', JSON.stringify(data))
    })
  }

  function getFilenameStorage() {
    return JSON.parse(localStorage.getItem('filenames'))
  }

  $(document).on('click', '#edit-button', function () {
    $.ajax({
      method: 'GET',
      url: window.location.pathname,
      data: {
        edit: 1, // purge and keep out of cache temporarily
      }
    }) // no callback on this.
  })

</script>
